package com.gdth.bank.presenter;

import com.gdth.bank.model.LoginModel;
import com.gdth.bank.model.TAppYhModel;
import com.gdth.bank.utils.LogUtils;
import com.gdth.bank.view.LoginView;
import com.gdth.database.bean.TAppYh;
import com.gdth.http.entity.HttpTokenResult;
import com.gdth.utils.LogUtil;
import com.google.gson.Gson;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/** 
 * @author  arvin
 * @version 2016-5-27 11:42:26 
 * 
 */
public class LoginPresenter implements IBasePresenter {
	public int update_login_key = 100;
	public int update_login = 101;
	public int update_login_lixian = 102;
	private LoginView mLoginView;
    private LoginModel mLoginModel;
    private TAppYhModel mTAppYhModel;

    public LoginPresenter(LoginView mLoginView) {
        this.mLoginView = mLoginView;
        mLoginModel = new LoginModel();
        this.mTAppYhModel = new TAppYhModel();
    }
    
    /**
     * 登录前从服务端请求公有密钥
     * @param username
     */
    public void getTokenForLogin(String username , String mm){
    	mLoginView.showProgressDialog("正在登录...");
    	
    	mLoginModel.getTokenForLogin(username,mm)
			.subscribeOn(Schedulers.io())
			.unsubscribeOn(Schedulers.io())
			.observeOn(AndroidSchedulers.mainThread())
			.subscribe(new Subscriber<HttpTokenResult>(){

				@Override
				public void onCompleted() {
					// TODO Auto-generated method stub
					if (!this.isUnsubscribed()) {
			            this.unsubscribe();
			        }
				}
	
				@Override
				public void onError(Throwable arg0) {
					// TODO Auto-generated method stub

					mLoginView.hideProgressDialog();
					
					if (arg0 != null){
						if(arg0.getMessage().equals("401没有获取当前登录手机的唯一编码,请设置手机权限")){
							mLoginView.AlertDialog();
						}else{
		    			mLoginView.showMessage(arg0.getMessage());}
					} else {
		    			mLoginView.showMessage("获取密钥失败！");
					}
					
//					mLoginView.loginByLocal();
				}
	
				@Override
				public void onNext(HttpTokenResult arg0) {
					// TODO Auto-generated method stub
					TAppYh user = mLoginView.getUser();

	    			login(user);
				}
		});
    }
    public long startlogin = 0;
    /**
     * 根据用户信息登录
     * @param user
     */
    public void login(TAppYh user){
    	mLoginView.showProgressDialog("正在登录...");
		startlogin = System.currentTimeMillis();
		LogUtils.d("_________登陆开始",String.valueOf(startlogin));
    	mLoginModel.login(user)
//    	主要改变的是订阅的线程。即call()执行的线程
			.subscribeOn(Schedulers.io())
			.unsubscribeOn(Schedulers.io())
//			主要改变的是发送的线程。即onNext()执行的线程。
			.observeOn(AndroidSchedulers.mainThread())
			.subscribe(new Subscriber<String>(){

				@Override
				public void onCompleted() {
					// TODO Auto-generated method stub
					mLoginView.hideProgressDialog();

					if (!this.isUnsubscribed()) {
			            this.unsubscribe();
			        }
				}
	
				@Override
				public void onError(Throwable arg0) {
					// TODO Auto-generated method stub
					if (arg0 != null){
		    			mLoginView.showMessage(arg0.getMessage());
					} else {
		    			mLoginView.showMessage("登录失败！");
					}
					mLoginView.hideProgressDialog();
				}
	
				@Override
				public void onNext(String arg0) {
					// TODO Auto-generated method stub
					TAppYh user = new TAppYh();
	            	Gson mGson = new Gson();
	            	user = mGson.fromJson(arg0, user.getClass());
	            	
	            	saveUserInformation(user);
					LogUtils.d("_________登陆结束",String.valueOf((startlogin -System.currentTimeMillis())));
				}
		});
    }
    
    /**
     * 获取到用户信息并保存本地
     * @param user
     */
    public void saveUserInformation(TAppYh user){
    	mTAppYhModel.saveUserInformation(user)
		.subscribeOn(Schedulers.io())
		.unsubscribeOn(Schedulers.io())
		.observeOn(AndroidSchedulers.mainThread())
		.subscribe(new Subscriber<TAppYh>(){

			@Override
			public void onCompleted() {
				// TODO Auto-generated method stub
				mLoginView.showMessage("登陆成功");
			}

			@Override
			public void onError(Throwable arg0) {
				// TODO Auto-generated method stub
				mLoginView.showMessage(arg0.getMessage());
			}

			@Override
			public void onNext(TAppYh arg0) {
				// TODO Auto-generated method stub
				mLoginView.loginSuccess(arg0);
			}
			
		});
    }
    
    /**
     * 离线登陆
     * @param user
     */
    public void loginByLocal(TAppYh user){
    	mTAppYhModel.getUserByLocal(user)
			.subscribeOn(Schedulers.io())
			.unsubscribeOn(Schedulers.io())
			.observeOn(AndroidSchedulers.mainThread())
			.subscribe(new Subscriber<TAppYh>(){

				@Override
				public void onCompleted() {
					// TODO Auto-generated method stub
					mLoginView.hideProgressDialog();
					mLoginView.showMessage("登陆成功");
				}

				@Override
				public void onError(Throwable arg0) {
					// TODO Auto-generated method stub
					mLoginView.hideProgressDialog();
					mLoginView.showMessage(arg0.getMessage());
				}

				@Override
				public void onNext(TAppYh arg0) {
					// TODO Auto-generated method stub
					mLoginView.loginSuccess(arg0);
				}
				
			});
    }
    
//    /**
//     * 保存到本地
//     * @param user
//     */
//    public void saveLoginInfo(User user){
//    	mLoginModel.update(user);
//    }

	@Override
	public void subscribe() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void unsubscribe() {
		// TODO Auto-generated method stub
		
	}
}
